//
//  PPCreateAlbumTableViewAlbumCell.m
//  pickpic
//
//  Created by Jinxiao on 8/28/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPAlbumContentTableViewPictureRowCell.h"

#import "PPAlbumContentImageView.h"

#define kEdgeInsets UIEdgeInsetsMake(10, 14, 0, 8)

#define kSpacing 3

@interface PPAlbumContentTableViewPictureRowCell ()
@property (readwrite, nonatomic, strong) PPAlbumContentImageView *imageView1;
@property (readwrite, nonatomic, strong) PPAlbumContentImageView *imageView2;
@property (readwrite, nonatomic, strong) PPAlbumContentImageView *imageView3;
@property (readwrite, nonatomic, strong) PPAlbumContentImageView *imageView4;
@end

@implementation PPAlbumContentTableViewPictureRowCell

- (void)awakeFromNib {
    [super awakeFromNib];

    self.selectionStyle = UITableViewCellSelectionStyleNone;

    _imageView1 = [[PPAlbumContentImageView alloc] init];
    _imageView1.backgroundColor = [UIColor clearColor];
    _imageView1.contentMode = UIViewContentModeScaleAspectFill;
    _imageView1.clipsToBounds = YES;
    [self addSubview:_imageView1];

    _imageView2 = [[PPAlbumContentImageView alloc] init];
    _imageView2.backgroundColor = [UIColor clearColor];
    _imageView2.contentMode = UIViewContentModeScaleAspectFill;
    _imageView2.clipsToBounds = YES;
    [self addSubview:_imageView2];

    _imageView3 = [[PPAlbumContentImageView alloc] init];
    _imageView3.backgroundColor = [UIColor clearColor];
    _imageView3.contentMode = UIViewContentModeScaleAspectFill;
    _imageView3.clipsToBounds = YES;
    [self addSubview:_imageView3];

    _imageView4 = [[PPAlbumContentImageView alloc] init];
    _imageView4.backgroundColor = [UIColor clearColor];
    _imageView4.contentMode = UIViewContentModeScaleAspectFill;
    _imageView4.clipsToBounds = YES;
    [self addSubview:_imageView4];

    __weak typeof(self)weakSelf = self;
    [_imageView1.deleteButton setActionBlock:^(UIControl *control) {
        [weakSelf.delegate pictureCell:weakSelf didSelectDeleteButton:(UIButton *)control atIndex:0];
    } forControlEvents:UIControlEventTouchUpInside];

    [_imageView1.control setActionBlock:^(UIControl *control) {
        [weakSelf.delegate pictureCell:weakSelf didSelectArtworkAtIndex:0];
    } forControlEvents:UIControlEventTouchUpInside];


    [_imageView2.deleteButton setActionBlock:^(UIControl *control) {
        [weakSelf.delegate pictureCell:weakSelf didSelectDeleteButton:(UIButton *)control atIndex:1];
    } forControlEvents:UIControlEventTouchUpInside];

    [_imageView2.control setActionBlock:^(UIControl *control) {
        [weakSelf.delegate pictureCell:weakSelf didSelectArtworkAtIndex:1];
    } forControlEvents:UIControlEventTouchUpInside];


    [_imageView3.deleteButton setActionBlock:^(UIControl *control) {
        [weakSelf.delegate pictureCell:weakSelf didSelectDeleteButton:(UIButton *)control atIndex:2];
    } forControlEvents:UIControlEventTouchUpInside];

    [_imageView3.control setActionBlock:^(UIControl *control) {
        [weakSelf.delegate pictureCell:weakSelf didSelectArtworkAtIndex:2];
    } forControlEvents:UIControlEventTouchUpInside];


    [_imageView4.deleteButton setActionBlock:^(UIControl *control) {
        [weakSelf.delegate pictureCell:weakSelf didSelectDeleteButton:(UIButton *)control atIndex:3];
    } forControlEvents:UIControlEventTouchUpInside];

    [_imageView4.control setActionBlock:^(UIControl *control) {
        [weakSelf.delegate pictureCell:weakSelf didSelectArtworkAtIndex:3];
    } forControlEvents:UIControlEventTouchUpInside];
}

- (void)layoutSubviews {
    [super layoutSubviews];

    _imageView1.frame = [[self class] tileFrameForIndex:0 boundingSize:CGSizeMake(self.width, CGFLOAT_MAX)];
    _imageView2.frame = [[self class] tileFrameForIndex:1 boundingSize:CGSizeMake(self.width, CGFLOAT_MAX)];
    _imageView3.frame = [[self class] tileFrameForIndex:2 boundingSize:CGSizeMake(self.width, CGFLOAT_MAX)];
    _imageView4.frame = [[self class] tileFrameForIndex:3 boundingSize:CGSizeMake(self.width, CGFLOAT_MAX)];
}

- (void)setArtwork1:(PPAbstractArtwork *)artwork1 {
    _artwork1 = artwork1;

    if(artwork1 == nil) {
        _imageView1.hidden = YES;
    } else {
        _imageView1.artwork = artwork1;
        _imageView1.hidden = NO;
    }
}

- (void)setArtwork2:(PPAbstractArtwork *)artwork2 {
    _artwork2 = artwork2;

    if(artwork2 == nil) {
        _imageView2.hidden = YES;
    } else {
        _imageView2.artwork = artwork2;
        _imageView2.hidden = NO;
    }
}

- (void)setArtwork3:(PPAbstractArtwork *)artwork3 {
    _artwork3 = artwork3;

    if(artwork3 == nil) {
        _imageView3.hidden = YES;
    } else {
        _imageView3.artwork = artwork3;
        _imageView3.hidden = NO;
    }
}

- (void)setArtwork4:(PPAbstractArtwork *)artwork4 {
    _artwork4 = artwork4;

    if(artwork4 == nil) {
        _imageView4.hidden = YES;
    } else {
        _imageView4.artwork = artwork4;
        _imageView4.hidden = NO;
    }
}

+ (CGFloat)heightForNumberOfPictures:(NSInteger)numberOfAlbums boundingSize:(CGSize)boundingSize {
    CGFloat width = (boundingSize.width - kEdgeInsets.left - kEdgeInsets.right - kSpacing*3)/4;
    CGFloat height = width + kSpacing/2*2;
    return height;
}

+ (CGRect)tileFrameForIndex:(NSInteger)index boundingSize:(CGSize)boundingSize {
    CGSize tileSize = [self tileSizeForBoundingSize:boundingSize];

    NSInteger column = index%4;

    return CGRectMake(kEdgeInsets.left + column*(tileSize.width + kSpacing), 0, tileSize.width, tileSize.height);
}

+ (CGSize)tileSizeForBoundingSize:(CGSize)boundingSize {
    CGFloat width = (boundingSize.width - kEdgeInsets.left - kEdgeInsets.right - kSpacing*3)/4;
    CGFloat height = width;

    return CGSizeMake(width, height);
}

@end
